
import glob
import re
import sys
import string
import pepe.p_txt.bigtxt as bt

space_fill = lambda x, y: x + ((y - len(x)) * ' ')

def write_results(output_data, file_handle):
    """ """
    o = output_data
    of = file_handle
    for k in sorted(o):
        #print space_fill(str(res[k]), 20), "\t:\t", k
        r = "%s\t%s\t:\t%s%s" % (space_fill('line_mask', 20), 
            space_fill(str(o[k][0]), 20), k, '\x0D\x0A')
        of.write(r)

        r = "%s\t%s\t:\t%s%s" % (space_fill('line_example', 20), 
            space_fill(str(o[k][0]), 20), o[k][1], '\x0D\x0A')
        of.write(r)


def main(input_file_mask):
    """ """
    files = glob.glob(input_file_mask)[:]

    # if multiple source files, open only one output
    if len(files) > 1:
        of = open('rowsmask.#','wb')

    o = {}

    for filename in files:
    
        fname = string.join(string.split(filename, '.')[0:-1], '.')
        print '    rowsmask: ', fname, '\n'
    
        for line in bt.lines(filename):
            line = re.sub('\x0D\x0A', '', line)
            line = re.sub('\x0D', '', line)
            line = re.sub('\x0A', '', line)

            # inspect full row
            mask = line[:]

            # 2011-07-26 as
            ## added more characters in the mask and reversed the logic
            #mask = re.sub('[A-Z]', 'X', mask)
            #mask = re.sub('[a-z]', 'X', mask)
            #mask = re.sub('\d', 'X', mask)
            #mask = re.sub('[-/&(),$|]', 'X', mask)
            mask = re.sub('(?!\s|\.|\:|\;).', 'X', mask)

            if o.has_key(mask):
                o[mask][0] += 1
                o[mask][1] = line[:]
            else:
                o[mask] = [1, '']
                o[mask][1] = line[:]

        # 2010-10-22_2257 vp
        ## using underscore instead dot because .# utilities 
        ## cut out everything after dot and then is not clear 
        ## whether utility was run on original file or on output
        ## of other .# utility that processed given file
        fname = string.split(filename, '.')[0]
        #2011-09-07_1519 vp
        #fname = re.sub('[^A-Za-z0-9]', '_', fname)
        #2011-09-07_1641 vp
        if len(files) == 1:
            of = open(fname + '_rowsmask.#','wb')
            r = 'type\tobservations\t_\tmask_or_example'
            r += '\t_\t_\t\x0D\x0A'.upper()
            of.write(r)
            write_results(output_data = o, file_handle = of)

    if len(files) > 1:
        r = 'type\tobservations\t_\tmask_or_example'
        r += '\t_\t_\t\x0D\x0A'.upper()
        of.write(r)
        write_results(output_data = o, file_handle = of)

    of.close()

if __name__ == '__main__':
    """ """
    if len(sys.argv) == 2:
        main(sys.argv[1])
    else:
        
        print """
        rowsmask <input-file-mask>
        example : rowsmask *.dat
        input-file-mask (mandatory)
            input data file(s) to be processed
        """
